//
//  AppDelegate.m
//  oc微博
//
//  Created by 安陆与 on 16/1/13.
//  Copyright © 2016年 安陆与. All rights reserved.
//

#import "AppDelegate.h"
#import "SinaTabBarViewController.h"
#import "SinaNewFeatureViewController.h"
#import "SinaWelcomeViewController.h"
#import "SinaAccountViewModel.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];
    
    [UINavigationBar appearance].tintColor = [UIColor orangeColor];
    
    //添加一个通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeVC:) name:@"change" object:nil];
    
    SinaTabBarViewController *tabBarVC = [[SinaTabBarViewController alloc]init];
//
    SinaNewFeatureViewController *NewFeature = [[SinaNewFeatureViewController alloc]init];
//
    SinaWelcomeViewController *welcome = [[SinaWelcomeViewController alloc]init];
    
    if ([[SinaAccountViewModel alloc]init].access_token) {
        self.window.rootViewController = welcome;

    }else{
        self.window.rootViewController = NewFeature;

    
    }
    
    
    return YES;

}



//通知处理方法
-(void)changeVC:(NSNotification *)noti{

    SinaTabBarViewController *tabBarVC = [[SinaTabBarViewController alloc]init];

    SinaWelcomeViewController *welcome = [[SinaWelcomeViewController alloc]init];

    if (noti.object == nil) {
        self.window.rootViewController = tabBarVC;
    }else{
    
        self.window.rootViewController = welcome;
    }
}


-(UIViewController *)returnDefaultController{


    return ([self checkVerson] ? ([[SinaNewFeatureViewController alloc]init]):([[SinaWelcomeViewController alloc]init]));
}

//检测版本的方法
-(BOOL)checkVerson{
    
    NSString *key = @"version";
    
    NSString *oldVersion = [[NSUserDefaults standardUserDefaults]objectForKey:key];
    
    NSString *newVersion = [[NSBundle mainBundle]infoDictionary][@"CFBundleShortVersionString"];
    
    //保存一下新版本
    [[NSUserDefaults standardUserDefaults]setObject:newVersion forKey:key];
    if (oldVersion) {
        
        if ([newVersion compare:oldVersion] == NSOrderedDescending) {
            
            return YES;
        }else{
        
            return NO;
        }
    }
    
    return YES;

}

//移除通知
-(void)dealloc{

    [self removeObserver:self forKeyPath:@"change"];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
